package com.itheima.server.service;

import com.itheima.domain.db.UserFreeze;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.mongo.Comment;
import com.itheima.domain.mongo.Publish;
import com.itheima.domain.vo.CommentVo;
import com.itheima.domain.vo.ErrorResult;
import com.itheima.domain.vo.PageResult;
import com.itheima.dubbo.api.CommentApi;
import com.itheima.dubbo.api.PublishApi;
import com.itheima.dubbo.api.UserFreezeApi;
import com.itheima.dubbo.api.UserInfoApi;
import com.itheima.server.client.UserFreezeClient;
import com.itheima.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.Reference;
import org.bson.types.ObjectId;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 阿鹏
 * @date 2021/1/29 21:08
 */
@Service
public class CommentsService {

    @Reference
    private CommentApi commentApi;
    @Reference
    private PublishApi publishApi;
    @Reference
    private UserInfoApi userInfoApi;
    @Autowired
    private MovementMQService movementMQService;
    @Autowired
    @Lazy
    private UserFreezeClient client;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    public ResponseEntity findComments(String publishId, int page, int pagesize) {
        PageResult<Comment> pageResult = commentApi.findComments(page, pagesize, publishId);
        List<Comment> commentList = pageResult.getItems();
        List<CommentVo> voList = new ArrayList<>();
        for (Comment comment : commentList) {
            CommentVo vo = new CommentVo();
            BeanUtils.copyProperties(comment, vo);
            vo.setId(comment.getId().toHexString());
            vo.setCreateDate(new DateTime(comment.getCreated()).toString("yyyy年MM月dd日 HH:mm"));

            Boolean hasLike = redisTemplate.hasKey("publish_comment_like_" + UserHolder.getUserId() + "_" + comment.getId().toHexString());
            vo.setHasLiked(hasLike ? 1 : 0);

            UserInfo userInfo = userInfoApi.findById(comment.getUserId());
            BeanUtils.copyProperties(userInfo, vo);

            voList.add(vo);
        }

        PageResult<CommentVo> voPageResult = new PageResult<>();
        BeanUtils.copyProperties(pageResult, voPageResult);
        voPageResult.setItems(voList);

        movementMQService.commentPublish(publishId);

        return ResponseEntity.ok(voPageResult);
    }

    public ResponseEntity saveComments(String publishId, String content) {
        Long userId = UserHolder.getUserId();
        String str = redisTemplate.opsForValue().get("USER_PREFIX_" + userId);
        if (str != null) {
            UserFreeze userFreeze = client.findByUserId(userId);
            // 判断是否是冻结发言
            if (userFreeze.getFreezingRange() == 2) {
                return ResponseEntity.status(500).body(ErrorResult.commentInvalid());
            }
        }

        // 封装评论数据
        Comment comment = new Comment();
        comment.setPublishId(new ObjectId(publishId));
        comment.setUserId(UserHolder.getUserId());
        comment.setContent(content);
        // 封装类型  2评论
        comment.setCommentType(2);
        // 1：对动态评论
        comment.setPubType(1);

        Publish publish = publishApi.findById(publishId);
        comment.setPublishUserId(publish.getUserId());

        commentApi.save(comment);
        return ResponseEntity.ok(null);
    }
}
